// 抽奖
(function() {
	var db = require("../db/index");
	var config = require("../_include/config");
	var db_table_name = config.dbname.busLottery;
	var db_id_prefix = config.dbIdPrefix.busLottery;
	var helper = require("../_include/helper");

	//构造函数
	var _con = function(info) {
		var self = this;
		//ID编号
		self._id = db_id_prefix + Math.random().toString(16).substr(2) + (+new Date).toString(16);

		self.state = _con.STATE.未开始;

		//进度，抽奖的人数
		self.joined_numner = 0;
		//参与人ID与参与时间点记录
		self.joiner_map = {};

		//对应的商家
		self.bus_id = info.bus_id;

		this.setInfo(info, "no_save");

		self.create_time = +new Date;

		db.insert(db_table_name, self);
		console.log("创建了一个抽奖活动：", info.title);
	};
	//PS:模块的暴露放在require skill前面，因为二者互相依赖
	module.exports = _con;
	//技能
	_con.prototype = require("./skill");

	_con.TYPE = {
		优惠券: "ticket_factory",
		商品: "goods",
		自定义:"_custom_award"
	};
	_con.STATE = {
		未开始: 0,
		进行中: 1,
		结束: 2
	};

	//静态方法
	_con.get = function(Id) {
		var result = db.find_by_id(db_table_name, Id);
		return _con.getInstance(result);
	};
	_con.getInstance = function(obj) {
		if (obj) {
			if (!(obj.nojson_able_keys instanceof Array)) { //固定黑名单
				Object.defineProperty(obj, "nojson_able_keys", {
					enumerable: false,
					value: ["joiner_map"]
				});
			}
			obj.__proto__ = _con.prototype;
		}
		return obj;
	};
}());